package com.tojabo.swing.ten_fileChooser;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class ChangePanel extends JPanel {

	private static final long serialVersionUID = 1L;

	int position_x = 20;
	int position_y = 50;

	private JFrame frame;

	private JTextField text;
	private JButton file_but;
	private JButton change_but;
	private JLabel lab;
	private JFileChooser fileChooser;

	private JButton exit_but;
	private JButton full_but;

	private Image backImage;

	public ChangePanel(JFrame fram) {
		this.setLayout(null);
		this.frame = fram;

		lab = new JLabel("路径：");
		text = new JTextField(20);
		file_but = new JButton("浏览...");
		change_but = new JButton("改变");

		exit_but = new JButton("退出");
		full_but = new JButton("FullScreen");

		fileChooser = new JFileChooser("E:\\");

		backImage = new ImageIcon("E:/图片/桌面/1.jpg").getImage();

		this.add(lab);
		this.add(file_but);
		this.add(text);
		this.add(change_but);
		this.add(exit_but);
		this.add(full_but);

		init();
	}

	private void init() {

		this.setBackground(Color.BLUE);

		lab.setBounds(position_x, position_y, 80, 28);
		text.setBounds(position_x + 60, position_y, 250, 28);
		file_but.setBounds(position_x + 320, position_y, 80, 28);

		change_but.setBounds(position_x + 50, position_y + 80, 80, 28);

		exit_but.setBounds(position_x + 50, position_y + 120, 80, 28);
		full_but.setBounds(position_x + 150, position_y + 120, 80, 28);

		change_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				change_Action(e);
			}
		});

		file_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				file_Action(e);
			}
		});

		exit_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				System.exit(0);
			}
		});
		full_but.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				full_Action(e);
			}
		});
	}

	protected void full_Action(MouseEvent e) {
		if (full_but.getText().equals("FullScreen")) {
			frame.dispose();
			frame.setUndecorated(true);
			frame.getGraphicsConfiguration().getDevice().setFullScreenWindow(frame);
			frame.setVisible(true);
			full_but.setText("NormalMode");
		} else {
			frame.dispose();
			frame.setUndecorated(false);
			frame.getGraphicsConfiguration().getDevice()
					.setFullScreenWindow(null);
			frame.setVisible(true);
			full_but.setText("FullScreen");
		}
	}

	protected void file_Action(MouseEvent e) {
		fileChooser.setApproveButtonText("确定");
		fileChooser.setDialogTitle("打开文件");
		File file = null;
		int result = fileChooser.showOpenDialog(this);

		if (result == JFileChooser.APPROVE_OPTION) {
			file = fileChooser.getSelectedFile();
			text.setText(file.getPath());

		}

	}

	protected void change_Action(MouseEvent e) {
		String imgPath = text.getText();
		File file = new File(imgPath);

		File filelist = new File(file.getParent());

		File files[] = filelist.listFiles();

		int index = (int) (Math.random() * files.length);

		backImage = new ImageIcon(files[index].getAbsolutePath()).getImage();
		this.repaint();
	}

	@Override
	protected void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		// ImageIcon back_icon = new ImageIcon();
		g2.drawImage(backImage, 0, 0, null);
	}

}
